﻿/*
	Copyright 2008 CATsoft Development GmbH

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
/*
	XML Parser
*/

/***********************************************************************
 Simple XML Parser
***********************************************************************/
function SimpleXmlParser(xmlDoc) {
	this.xmlDoc = xmlDoc;  
  this.parseError = xmlDoc.parseError;
  if (this.parseError.errorCode != 0) {
		showAlert("SimpleXmlParser ERROR: ",  this.parseError.reason);
	}

/***********************************************************************
	Check Error
***********************************************************************/
SimpleXmlParser.prototype.checkError = function(checkonly){
  var xmlDoc = this.xmlDoc;
  var items = [];
	var value='';
  if (this.parseError.errorCode != 0) {
		showAlert("SimpleXmlParser ERROR: ",  this.parseError.reason);
  } 
	else 
	{
		var objNodeList = xmlDoc.getElementsByTagName("err");
	  if (objNodeList.length == 1)
		{
			var xmlItem = objNodeList.item(0);
			value = xmlItem.getAttribute('msg');
			
		}
	}

	if (value.length > 0 ) 
		if(checkonly)
			debug.info("Info: " + value)
		else
			debug.error("Error: " + value);
  return value;
}

/***********************************************************************
 Get Child value
***********************************************************************/
SimpleXmlParser.prototype.getValue = function(key)
{
  var xmlDoc = this.xmlDoc;
  
  if (this.parseError.errorCode != 0) {
		showAlert("SimpleXmlParser ERROR: ", this.parseError.reason);
  } else {
		var objNodeList = xmlDoc.getElementsByTagName(key);
	  if (objNodeList.length >= 1)
		{
			
			var xmlItem = objNodeList.item(0);
			var child = xmlItem.childNodes.item(0);			
			value = child.nodeValue;
		}
	}
  return value;
}

/***********************************************************************
	Get Attribute
***********************************************************************/
SimpleXmlParser.prototype.getAttributeValue = function(key, attribute)
{
  var xmlDoc = this.xmlDoc;
  
  if (this.parseError.errorCode != 0) {
		showAlert("SimpleXmlParser ERROR: ", this.parseError.reason);
  } else {
		var objNodeList = xmlDoc.getElementsByTagName(key);
	  if (objNodeList.length >= 1)
		{
			
	  var xmlItem = objNodeList.item(0);
			value = xmlItem.getAttribute( attribute );
		}
	}
  return value;
}

/***********************************************************************
	Parse Feed
***********************************************************************/
SimpleXmlParser.prototype.parseFeed = function(url) {
    var xmlDoc = this.xmlDoc;
    var maxItems = parseInt(options.getValue("maxitems"));
    var items = 1;

    if (this.parseError.errorCode != 0) {
        showAlert("SimpleXmlParser ERROR: ", this.parseError.reason);
        return 0;
    }
    else {

        debug.trace(url);
        //traverse( xmlDoc);
        var objNodeList = xmlDoc.getElementsByTagName("item");
        for (var i = 0; i < objNodeList.length; ++i) {
            var xmlItem = objNodeList.item(i);
            var item
            item = new Object();
            item.feedurl = url;
            temp = xmlItem.getElementsByTagName("title");
            item.title = xmlEscape(temp[0].childNodes.item(0).nodeValue);
            temp = xmlItem.getElementsByTagName("description");
            item.description = xmlEscape(temp[0].childNodes.item(0).nodeValue);
            temp = xmlItem.getElementsByTagName("link");
            item.link = xmlEscape(temp[0].childNodes.item(0).nodeValue);
            temp = xmlItem.getElementsByTagName("pubDate");
            item.date = Date.parse(temp[0].childNodes.item(0).nodeValue);
            item.pub = temp[0].childNodes.item(0).nodeValue;
            item.read = false;
            item.hash = hex_md5(item.link);
            addItem(item)
            debug.trace(item.title);
            if (items >= maxItems)
                break;
            items++;
        }
        if (objNodeList.length == 0)		// check for atom feed
        {
            //traverse( xmlDoc);
            var objNodeList = xmlDoc.getElementsByTagName("entry");
            for (var i = 0; i < objNodeList.length; ++i) {
                var xmlItem = objNodeList.item(i);
                var item;
                var dateString;
                item = new Object();
                item.feedurl = url;
                temp = xmlItem.getElementsByTagName("title");
                item.title = xmlEscape(temp[0].childNodes.item(0).nodeValue);

                temp = xmlItem.getElementsByTagName("content");
                item.description = xmlEscape(temp[0].childNodes.item(0).nodeValue);

                temp = xmlItem.getElementsByTagName("link");
                for (var j = 0; j < temp.length; ++j) {
                    if (temp[j].getAttribute("rel") == "self") {
                        item.link = temp[j].getAttribute("href");
                        break;
                    }
                }
                temp = xmlItem.getElementsByTagName("published");
                dateString = temp[0].childNodes.item(0).nodeValue;
                item.date = rfc3339StringToDate(dateString);
                item.pub = temp[0].childNodes.item(0).nodeValue;
                item.read = false;
                item.hash = hex_md5(item.link);
                addItem(item)
                debug.trace(item.title);
                if (items >= maxItems)
                    break;
                items++;
            }
        }
        return objNodeList.length;
    }

}
}

